<template>
  <view>
    <!-- 搜索 -->
    <my-search bgcolor="#c00000" radius="16" @gotoSearch="gotoSearch"></my-search>
    <!-- 分类 -->
    <view class="cate-container">
      <!-- 左边 -->
      <scroll-view class="cate-left" scroll-y="true" :style="{height: wh+'px'}" >
        <view
          :class="['cate-left-item', i===active ? 'active' : '']"
          v-for="(item,i) in cateList"
          :key="i"
          @click="activeChanged(i)"
        >
          {{item.cat_name}}
        </view>
      </scroll-view>
      <!-- 右边 -->
      <scroll-view class="cate-right" scroll-y="true" :style="{height: wh+'px'}" :scroll-top="scrollTop">
        <view v-for="(item, i) in cateLevel2" :key="i">
          <!-- title -->
          <view class="cate-right-title">
            / {{item.cat_name}} /
          </view>
          <!-- 分类详情 -->
          <view class="cate-right-list">
            <view class="cate-right-list-item" v-for="(item2, i2) in item.children" :key="i2" @click="gotoGoodsList(item2)">
              <image :src="item2.cat_icon"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
    
  </view>
</template>

<script>
  
  // 导入设置徽标mixins方法
  import badgeMix from '@/mixins/tabbar-badge.js'
  
  import { mapState } from 'vuex'
  
  export default {
    mixins: [badgeMix],
    data() {
      return {
        // 页面UI配置数据
        wh: 0, // 手机屏幕可使用高度
        active: 0, // 左边分类选中索引
        scrollTop: 0, //活动组件到顶部距离
        // 页面显示数据
        cateList: [], // 一级分类数据
        cateLevel2: [], // 二级分类数据
      };
    },
    computed:{
      ...mapState('m_cart', ['cart'])
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      console.log(sysInfo)
      // this.wh = 200
      this.wh = sysInfo.windowHeight - 50
      
      // 发起分类数据请求
      this.getCateList()
    },
    methods: {
      // 获取分类数据
      async getCateList() {
        const {data: res} = await uni.$http.get('/categories')
        console.log(res)
        if(res.meta.status !== 200) return uni.$showMsg()
        //赋值数据到data中
        this.cateList = res.message
        // 二级分类数据
        this.cateLevel2 = res.message[0].children
      },
      activeChanged(i){
        this.active = i
        
        // 修改level2的数据
        this.cateLevel2 = this.cateList[i].children
        
        // 
        this.scrollTop = Math.random()
      },
      gotoGoodsList(item) {
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?cid=${item.cat_id}`
        })
      },
      gotoSearch(){
        console.log('gotoSearch')
        // 跳转到搜索页面
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    },
  }
</script>

<style lang="scss">
  .cate-container {
    display: flex;
    .cate-left{
      width: 240rpx;
      .cate-left-item {
        width: 100%;
        height: 120rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #F7F7F7;
        font-size: 24rpx;
        &.active{
          background-color: #ffffff;
          display: flex;
          justify-content: space-between;
          &::before {
            content: '';
            width: 4rpx;
            height: 64rpx;
            background-color: #c00000;
          }
          &::after {
            content: '';
            width: 4rpx;
            height: 64rpx;
            background-color: #ffffff;
          }
        }
      }
    }
    
    .cate-right{
      .cate-right-title{
        display: flex;
        justify-content: center;
        align-items: center;
        font-weight: bold;
      }
      .cate-right-list{
        display: flex;
        flex-wrap: wrap;
        .cate-right-list-item{
          width: 33.33%;
          // flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          image {
            width: 120rpx;
            height: 120rpx;
          }
          text {
            font-size: 24rpx;
          }
        }
      }
    }
  }
</style>
